package com.linln.modules.member.repository;

import java.util.Date;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;

import com.linln.modules.member.domain.MemberInfo;
import com.linln.modules.member.domain.MemberOrder;
import com.linln.modules.system.repository.BaseRepository;


/**
 * @author hct
 * @date 2019/09/12
 */
public interface MemberOrderRepository extends JpaRepository<MemberOrder, Long>  ,JpaSpecificationExecutor<MemberOrder>
{
	
	@Transactional
	@Query(value="select IFNULL(sum(money),0)  from member_order where pay_status=1 and type in (1,2,3,4) and area = ?1 ",nativeQuery=true)
	public Double getSumByArea(String area);
	
	
	@Transactional
	@Query(value="select IFNULL(sum(money),0)  from member_order where area = ?1 and to_days(create_date) = to_days(now()) ",nativeQuery=true)
	public Double getSumByAreaAndToday(String area);
	
	
	@Transactional
	@Query(value="select IFNULL(sum(money),0)  from member_order where pay_status=1 ",nativeQuery=true)
	public Double getSumByArea();
	
	@Transactional
	@Query(value="select IFNULL(sum(money),0)  from member_order where pay_status=1 and type=?1 ",nativeQuery=true)
	public Double getSumByArea(int type);
	
    public  MemberOrder findByOrderCode(String orderCode);

    /**
  	  * 确认已经支付，修改状态
  	  * @param orderId
  	  * @return
  	  */
     @Modifying
     @Transactional
     @Query("update MemberOrder set payStatus =1  where orderCode = ?1 and payStatus=0")
     public Integer updatePayStatus( String orderId);
     /**
  	  * 确认退款，修改状态
  	  * @param orderId
  	  * @return
  	  */
     @Modifying
     @Transactional
     @Query("update MemberOrder set payStatus =2  where orderCode = ?1 and payStatus=1")
     public Integer updateRefundStatus( String orderId);
      
}